<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div class="header-logo">
        <img src="../assets/logo.png" alt />
        电商后台管理系统
      </div>

      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
        </div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#4091ff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 index 只接收字符串 -->
          <el-submenu :index="'/' + item.path" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      // 在 for 循环中添加不同的图标
      iconsObj: {
        "125": "iconfont icon-users",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      // 菜单是否折叠
      isCollapse: false,

      // 被激活的链接地址
      activePath: ''
    };
  },

  // 在整个页面刚一加载的时候就触发
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    // 点击退出按钮
    logout() {
      // 清空 token
      window.sessionStorage.clear();
      // 跳转到登录页
      this.$router.push("/login");
    },

    // 获取左侧菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      // console.log(this.menuList);
    },

    // 点击按钮，切换菜单折叠展开
    toggleCollapse() {
        this.isCollapse = !this.isCollapse
    },

    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;

  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #373d41;

    .header-logo {
      line-height: 60px;
      font-size: 20px;
      color: #ffffff;

      img {
        margin: 0 10px 0 0;
        width: 60px;
        float: left;
      }
    }
  }

  .el-container {
    .el-aside {
      background: #333744;

      .toggle-button {
        line-height: 24px;
        letter-spacing: 0.2em;
        text-align: center;
        color: #fff;
        background: #4a5064;
        cursor: pointer;
      }

      .el-menu {
        border-right: none;
      }

      .iconfont {
        margin-right: 8px;
      }
    }

    .el-main {
      background: #eaedf1;
    }
  }
}
</style>